package com.vip8.recorder.service.web.controller;

import com.vip8.recorder.api.facade.UserFacade;
import com.vip8.recorder.api.model.base.MultiResult;
import com.vip8.recorder.api.model.base.SingleResult;
import com.vip8.recorder.api.model.dto.UserDTO;
import com.vip8.recorder.api.model.dto.VerificationCodeDTO;
import com.vip8.recorder.api.model.params.LoginParam;
import com.vip8.recorder.api.model.params.UserParams;
import com.vip8.recorder.service.service.UserService;
import com.vip8.recorder.service.utils.JsonUtil;
import com.vip8.recorder.service.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Validated
@Slf4j
public class UserController implements UserFacade {

    @Resource
    private UserService userService;

    @Override
    @RequestMapping(value = "/generateCode", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    public SingleResult generateCode(@RequestBody String loginParam) {
        LoginParam param = JsonUtil.parseObject(loginParam,  LoginParam.class);
        if (param == null) {
            return SingleResult.failure();
        }
        try {
            userService.generateCode(param.getCountryCode(), param.getPhone());
        } catch (Exception e) {
            return SingleResult.failure(e.getMessage());
        }
        return SingleResult.success();
    }


    @RequestMapping(value = "/getCode", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    public SingleResult<VerificationCodeDTO> getCode(@RequestBody String loginParam) {
        LoginParam param = JsonUtil.parseObject(loginParam,  LoginParam.class);
        if (param == null) {
            return SingleResult.failure();
        }
        VerificationCodeDTO codeDTO = null;
        try {
            codeDTO = userService.getCode(param.getCountryCode(), param.getPhone());
        } catch (Exception e) {
            return SingleResult.failure(e.getMessage());
        }
        return SingleResult.success(codeDTO);
    }

    /**
     * 工号密码登陆
     * @param loginParam
     * @return
     */
    @RequestMapping(value = "/loginByEmp", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    public SingleResult<UserDTO> loginByEmp(@RequestBody String loginParam) {
        try {
            UserParams params = JsonUtil.parseObject(loginParam, UserParams.class);
            UserDTO userDTO  = userService.loginByEmpInfo(params);
            if(userDTO == null){
                return SingleResult.failure("用户不存在");
            }
            return SingleResult.success(userDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return SingleResult.failure(e.getMessage());
        }
    }
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<UserDTO> login(@RequestBody String loginParam) {
        UserParams params = JsonUtil.parseObject(loginParam, UserParams.class);
        UserDTO userDTO  = userService.loginByDevice(params);
        if(userDTO == null){
            return SingleResult.failure("用户不存在");
        }
        return SingleResult.success(userDTO);
    }

//    @RequestMapping(value = "/loginByPhone", method = RequestMethod.POST)
//    @ResponseBody
//    @Validated
//    @Override
    public SingleResult<UserDTO> loginByPhone(@RequestBody String loginParam) {
        UserParams params = JsonUtil.parseObject(loginParam, UserParams.class);
        if(params == null || StringUtil.isEmpty(params.getPhone())){
            return  SingleResult.failure("参数不能为空");
        }
        UserDTO userDTO = userService.login(params.getPhone());
        if(userDTO == null){
            return SingleResult.failure("用户不存在");
        }
        return SingleResult.success(userDTO);
    }

    @RequestMapping(value = "/loginByCode", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<UserDTO> loginByCode(@RequestBody String loginParam) {
        LoginParam params = JsonUtil.parseObject(loginParam, LoginParam.class);
        if(params == null || StringUtil.isEmpty(params.getPhone()) || StringUtil.isEmpty(params.getCode())){
            return  SingleResult.failure("参数不能为空");
        }
        UserDTO userDTO = null;
        try {
            userDTO = userService.loginByCode(params.getPhone(), params.getCode());
        }catch (Exception e){
            return SingleResult.failure(e.getMessage());
        }

        if(userDTO == null){
            return SingleResult.failure("用户不存在");
        }
        return SingleResult.success(userDTO);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<Boolean> logout(@RequestBody  String loginParam) {
        UserParams params = JsonUtil.parseObject(loginParam, UserParams.class);
        if(params == null || params.getPhone() == null){
            return SingleResult.failure("参数异常");
        }
        Boolean result  = userService.logout(params.getPhone());
        return SingleResult.success(result);
    }

    @RequestMapping(value = "/queryRecorderList", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<UserDTO> queryRecorderList(@RequestBody String loginParam) {
        UserParams params = JsonUtil.parseObject(loginParam, UserParams.class);
        List<UserDTO> userDTOS = userService.queryRecorderList(params);
        return MultiResult.success(userDTOS);
    }
}
